package com.template.controller;


import com.template.entity.Staff;
import com.template.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yumuyang
 * @since 2021-11-01
 */
@Controller
public class StaffController {

    @Autowired
    StaffService staffService;

    @GetMapping("/deleteStaff")
    public String deleteStaff(@RequestParam("id") Integer id,
                              @RequestParam(value = "pn", defaultValue = "1") Integer pn,
                              RedirectAttributes redirectAttributes) {
        staffService.deleteStaffById(id);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/staffList";
    }

    @PostMapping("/addStaff")
    public String addStaff(Staff staff) {
        staffService.saveStaff(staff);

        return "redirect:/staffList";
    }

    @PostMapping("/updateStaff")
    public String updateStaff(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                              Staff staff,
                              RedirectAttributes redirectAttributes) {
        staffService.updateStaff(staff);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/staffList";
    }

}

